package com.citycloud.ccuap.tc.admin.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletResponse;

import com.citycloud.ccuap.framework.util.DateUtil;

/***
 * @className WebUtils
 * @author wangyj
 * @Date 2019/12/18 12:24
 * @description
 */
public class WebUtils {
    
    public static void putResponse( ServletResponse servletResponse, String errorMsg, long code) throws IOException {
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("application/json;charset=UTF-8");
        OutputStream out = servletResponse.getOutputStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, "utf-8"));
        String jsonString = getErrorJsonString(errorMsg, code);
        pw.println(jsonString);
        pw.flush();
        pw.close();
    }
    
    public static String getErrorJsonString(String message, Long code) {
        String jsonString = "";
        if (message == null) {
            message = "";
        }
        
        jsonString = "{\"success\":false,\"code\":" + code + ",\"lastTime\":\"" + DateUtil.getDateTimeString(new Date()) + "\",\"message\":\"" + message.replace("'", "\\'") + "\"}";
        return jsonString;
    }
}
